﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Reflection;

namespace Dynamic_UI_Framework
{
    /// <summary>
    /// LayoutDialog.xaml 的交互逻辑
    /// </summary>
    public partial class LayoutDialog : Window
    {
        public LayoutDialog()
        {
            InitializeComponent();

            PropertyInfo[] brushEnum = typeof(Brushes).GetProperties();
            foreach (var item in brushEnum)
                this.cbBackground.Items.Add(item.Name);

            string[] dockEnum = Enum.GetNames(typeof(Dock));
            foreach (var item in dockEnum)
                this.cbDock.Items.Add(item);
        }

        public string TargetName
        {
            get { return this.txtName.Text.Trim(); }
            set { this.txtName.Text = value; }
        }

        public Dock TargetDock
        {
            get
            {
                if (string.IsNullOrEmpty(this.cbDock.Text))
                    return Dock.Left;
                return (Dock)Enum.Parse(typeof(Dock), this.cbDock.Text);
            }
            set { this.cbDock.Text = value.ToString(); }
        }

        public Brush TargetBackground
        {
            get
            {
                if (string.IsNullOrEmpty(this.cbBackground.Text))
                    return Brushes.White;
                return (Brush)new BrushConverter().ConvertFromString((this.cbBackground.Text.ToString()));
            }
            set { this.cbBackground.Text = value == null ? string.Empty : value.ToString(); }
        }

        public double TargetWidth
        {
            get
            {
                double v;
                bool ret = double.TryParse(this.txtWidth.Text, out v);
                return ret ? v : double.NaN;
            }
            set { this.txtWidth.Text = value.ToString(); }
        }

        public double TargetHeight
        {
            get
            {
                double v;
                bool ret = double.TryParse(this.txtHeigh.Text, out v);
                return ret ? v : double.NaN;
            }
            set { this.txtHeigh.Text = value.ToString(); }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(TargetName))
            {
                MessageBox.Show("Name 不可以为空");
                return;
            }

            this.DialogResult = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

    }
}
